//
//  HomeViewController.swift
//  ParkManage
//
//  Created by Jalon on 9/21/20.
//  Copyright © 2020 diligrp. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var dataArray: [HomeCellItem] = HomeCellItem.createHomeCellItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(R.nib.homeCollectionViewCell)
        collectionView.collectionViewLayout = HomeFlowLayout()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    @IBAction func logoutAction(_ sender: Any) {
        
        let alertView = PMAlertView(alertMsg: "是否退出登录？")
        alertView.showAlert(style: .sureAndCancelStyle)
        alertView.btnActionCallBack = { (tag) in
            if tag == 101 {
                Configs.Dimensions.AppWindow.rootViewController =
                    BaseNavigationController(rootViewController: LoginViewController())
            }
        }
    }
}

extension HomeViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: HomeCollectionViewCell = collectionView.dequeueReusableCell(
            withReuseIdentifier: R.reuseIdentifier.homeCollectionCell, for: indexPath
        )!
        let item = dataArray[indexPath.row]
        cell.homeCellModel = item
        return cell
    }
}

extension HomeViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let item: HomeCellItem = dataArray[indexPath.row]
        if item.id < 2 {
            navigator.push(item.navigator, context: item.id == 0 ? AuditType.pass : AuditType.departure)
        } else {
            navigator.push(item.navigator)
        }
    }
}
